//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FloatingTypePayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/TypePayoff.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (QL::Option::TypeEnum type) : CTypePayoff(CFloatingTypePayoff::typeid)
{
    try
    {
#ifdef HANDLE
        _phFloatingTypePayoff = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        _ppFloatingTypePayoff = new boost::shared_ptr<QuantLib::FloatingTypePayoff> (new QuantLib::FloatingTypePayoff ( _type ));
        SetTypePayoff (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (boost::shared_ptr<QuantLib::FloatingTypePayoff>& childNative, Object^ owner) : CTypePayoff(CFloatingTypePayoff::typeid)
{
#ifdef HANDLE
	_phFloatingTypePayoff = NULL;
#endif
	_ppFloatingTypePayoff = &childNative;
    _ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
}
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (QuantLib::FloatingTypePayoff& childNative, Object^ owner) : CTypePayoff(CFloatingTypePayoff::typeid)
{
#ifdef HANDLE
	_phFloatingTypePayoff = NULL;
#endif
	_ppFloatingTypePayoff = new boost::shared_ptr<QuantLib::FloatingTypePayoff> (&childNative);
    _ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
    _FloatingTypePayoffOwner = owner;
    _TypePayoffOwner = owner;
}

Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (CFloatingTypePayoff^ copy) : CTypePayoff(CFloatingTypePayoff::typeid)
{
#ifdef HANDLE
	_phFloatingTypePayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFloatingTypePayoff = new boost::shared_ptr<QuantLib::FloatingTypePayoff> (copy->GetShared());
        _ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
    }
}
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (PLATFORM::Type^ t) : CTypePayoff(CFloatingTypePayoff::typeid)
{
#ifdef HANDLE
	_phFloatingTypePayoff = NULL;
#endif
	if (!t->IsSubclassOf(CFloatingTypePayoff::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (QuantLib::Handle<QuantLib::FloatingTypePayoff>& childNative, Object^ owner)  : CTypePayoff(CFloatingTypePayoff::typeid)
{
	_phFloatingTypePayoff = &childNative;
	_ppFloatingTypePayoff = &static_cast<boost::shared_ptr<QuantLib::FloatingTypePayoff>>(childNative.currentLink());
    _ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
    _FloatingTypePayoffOwner = owner;
}
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (QuantLib::Handle<QuantLib::FloatingTypePayoff> childNative)  : CTypePayoff(CFloatingTypePayoff::typeid)
{
	_phFloatingTypePayoff = &childNative;
	_ppFloatingTypePayoff = &static_cast<boost::shared_ptr<QuantLib::FloatingTypePayoff>>(childNative.currentLink());
    _ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CFloatingTypePayoff::CFloatingTypePayoff (QuantLib::FloatingTypePayoff childNative)  : CTypePayoff(CFloatingTypePayoff::typeid)
{
#ifdef HANDLE
	_phFloatingTypePayoff = NULL;
#endif
	_ppFloatingTypePayoff = new boost::shared_ptr<QuantLib::FloatingTypePayoff> (new QuantLib::FloatingTypePayoff (childNative));
    _ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (boost::dynamic_pointer_cast<QuantLib::TypePayoff> (*_ppFloatingTypePayoff));
}
#endif

Cephei::QL::Instruments::CFloatingTypePayoff::~CFloatingTypePayoff ()
{
    if (_ppFloatingTypePayoff != NULL)
    {
	    delete _ppFloatingTypePayoff;
        _ppFloatingTypePayoff = NULL;
    }
}
Cephei::QL::Instruments::CFloatingTypePayoff::!CFloatingTypePayoff ()
{
    if (_ppFloatingTypePayoff != NULL)
    {
	    delete _ppFloatingTypePayoff;
    }
}
QuantLib::FloatingTypePayoff& Cephei::QL::Instruments::CFloatingTypePayoff::GetReference ()
{
    if (_ppFloatingTypePayoff == NULL) throw REFNEW NativeNullException ();
	return **_ppFloatingTypePayoff;
}
boost::shared_ptr<QuantLib::FloatingTypePayoff>& Cephei::QL::Instruments::CFloatingTypePayoff::GetShared ()
{
    if (_ppFloatingTypePayoff == NULL) throw REFNEW NativeNullException ();
	return *_ppFloatingTypePayoff;
}
QuantLib::FloatingTypePayoff* Cephei::QL::Instruments::CFloatingTypePayoff::GetPointer ()
{
    if (_ppFloatingTypePayoff == NULL) throw REFNEW NativeNullException ();
	return &**_ppFloatingTypePayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FloatingTypePayoff>& Cephei::QL::Instruments::CFloatingTypePayoff::GetHandle ()
{
	if (_phFloatingTypePayoff == NULL)
	{
		_phFloatingTypePayoff = new Handle<QuantLib::FloatingTypePayoff> (*_ppFloatingTypePayoff);
	}
	return *_phFloatingTypePayoff;
}
#endif
bool Cephei::QL::Instruments::CFloatingTypePayoff::HasNative () 
{
	return (_ppFloatingTypePayoff != NULL);
}

String^ Cephei::QL::Instruments::CFloatingTypePayoff::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppFloatingTypePayoff)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IFloatingTypePayoff^ Cephei::QL::Instruments::CFloatingTypePayoff_Factory::Create (QL::Option::TypeEnum type)
{
    return REFNEW CFloatingTypePayoff ( type);
}
